<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class comments extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
    }
    
    function index()
    {
        $this->tt = "评论管理";
        $products_id=$this->spArgs("products_id",0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 10);
        $obj = spClass("commentsModel");
        if($products_id==0){
         $conditions = array('1' => 1);   
        }else{
        $conditions = array('products_id' => $products_id);
        }
        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions, 'comments_id desc');
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->products_id=$products_id;
        $this->display("admin/comments_index.html");
    }
    function plist()
    {
        $this->display("admin/products_list.html");
    }
    function edit(){
       $this->size = array(0 => '鞋类', 1 => '包类', 2 => '衣服');
       $comments_id = $this->spArgs("comments_id", 0);
       $Obj=spClass('commentsModel');
       $conditions = array("comments_id"=>$comments_id); 
       $comments= $Obj->spLinker()->find($conditions);
       $tree = spClass('Tree');
       $fields = array('categories_id', 'parent_id');
       $tree->treelist($this->categories, $fields);
       $arr = $tree->leaf(0);
       $this->arr = $arr;
       $this->comments = $comments;
       $this->parent_id=$comments['master_categories_id'];
       $allimg = explode('|', $comments['comments_allimg']);
       $this->allimg=$allimg;
       $this->addtitle="修改信息";
       $this->display("admin/comments_add.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $data = array(
            "master_categories_id" => $this->spArgs("parent_id"),
            "detail" => array(
                        "comments_name" => $this->spArgs("title"),
                        "comments_description" => $this->spArgs("content"),
                        "comments_seokeywords" => $this->spArgs("comments_seokeywords"),
                        "comments_seodescription" => $this->spArgs("comments_seodescription"),
                        "comments_url" => $this->spArgs("comments_url",0)
                        ),
            "comments_allimg" => $this->spArgs("allimg"),
            "comments_image" => $this->spArgs("uploadList")
            );
        $Obj = spClass('commentsModel');    
        if ($strAction == 'add'){
          $data['comments_date_added']=time();
          $comments_id=$Obj->spLinker()->create($data);
          $msg = "评论ID:".$comments_id."添加成功";            
        }else
        {
            $data['comments_last_modified']=time();
            $comments_id =$this->spArgs("id"); 
            $condition = array('comments_id' => $comments_id);
            $Obj->spLinker()->update($condition, $data);
            $msg = "评论ID:".$comments_id."修改成功";     
        }
        Savelog($msg);
        $result = array('status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result);
    }
    function sh()
    {
        $Obj = spClass("commentsModel");
        $id2 = $this->spArgs("comments_id", 0);
        $products_id = $this->spArgs("products_id", 0);
        $type = $this->spArgs("type", 0);        
        $cid = $this->spArgs("cid", 0);
        $data=array('type'=>$type);        
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
            foreach ($idsarray as $id)
            {
                $conditions = array('comments_id' => $id);
                $Obj->update($conditions,$data); 
            }
        } else
        {
            $conditions = array('comments_id' => $id2);
            $Obj->update($conditions,$data); 
        }
        $msg='评论审核'.$id2;
        Savelog($msg);
        $this->success(T('Successfully edited!'), spUrl("comments", "index",array("products_id"=>"$products_id")));

    } 
    function del()
    {
        $Obj = spClass("commentsModel");
        $products_id = $this->spArgs("products_id", 0);
        $id2 = $this->spArgs("comments_id", 0);
        $cid = $this->spArgs("cid", 0);
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);

            foreach ($idsarray as $id)
            {
                $conditions = array('comments_id' => $id);
                $Obj->delete($conditions); // 删除记录
            }
        } else
        {
            $conditions = array('comments_id' => $id2);
            $Obj->delete($conditions); // 删除记录
        }
        $msg='评论删除'.$id2;
        $this->log($msg);
        $this->success(T('Successfully  deleted!'), spUrl("comments", "index",array("products_id"=>"$products_id")));
    }  
}